package com.linjiang.design.chainofresponsibility;

//责任链模式
//为请求创建一个接收者对象的链
public class ChainOfResponsibility {

    public static void main(String[] args) {
        Request.RequestBuilder requestBuilder = new Request.RequestBuilder();
        Request request = requestBuilder
                .loggedOn(true)
                .frequentOk(true)
                .isPermits(true)
                .containSensitiveWords(false)
                .requestBody("数据")
                .build();

        Handler requestContainSensitiveWordsHandler = new RequestContainSensitiveWordsHandler(null);
        Handler requestLoggedOnHandler = new RequestLoggedOnHandler(requestContainSensitiveWordsHandler);
        Handler requestPermitsHandler = new RequestPermitsHandler(requestLoggedOnHandler);
        Handler requestFrequentHandler = new RequestFrequentHandler(requestPermitsHandler);

        boolean process = requestFrequentHandler.process(request);
        if (process){
            System.out.println("请求验证通过，请求参数："+request.getRequestBody());
        }else {
            System.out.println("请求验证不通过");
        }
    }
}

class Request{
    private boolean loggedOn;
    private boolean frequentOk;
    private boolean isPermits;
    private boolean containSensitiveWords;

    public String getRequestBody() {
        return requestBody;
    }

    private String requestBody;

    public Request(boolean loggedOn, boolean frequentOk, boolean isPermits, boolean containSensitiveWords, String requestBody) {
        this.loggedOn = loggedOn;
        this.frequentOk = frequentOk;
        this.isPermits = isPermits;
        this.containSensitiveWords = containSensitiveWords;
        this.requestBody = requestBody;
    }

    public boolean isLoggedOn(){
        return this.loggedOn;
    }

    public boolean isFrequentOk(){
        return this.frequentOk;
    }

    public boolean isPermits(){
        return this.isPermits;
    }

    public boolean isContainSensitiveWords(){
        return this.containSensitiveWords;
    }

    static class RequestBuilder{
        private boolean loggedOn;
        private boolean frequentOk;
        private boolean isPermits;
        private boolean containSensitiveWords;
        private String requestBody;

        public Request build(){
            return new Request(this.loggedOn,this.frequentOk,this.isPermits, this.containSensitiveWords, this.requestBody);
        }
        public RequestBuilder loggedOn(boolean loggedOn){
            this.loggedOn = loggedOn;
            return this;
        }

        public RequestBuilder frequentOk(boolean frequentOk){
            this.frequentOk = frequentOk;
            return this;
        }

        public RequestBuilder isPermits(boolean isPermits){
            this.isPermits = isPermits;
            return this;
        }

        public RequestBuilder containSensitiveWords(boolean containSensitiveWords){
            this.containSensitiveWords = containSensitiveWords;
            return this;
        }

        public RequestBuilder requestBody(String requestBody){
            this.requestBody = requestBody;
            return this;
        }
    }
}

abstract class Handler{
    Handler next;

    public Handler(Handler next) {
        this.next = next;
    }

    public Handler getNext() {
        return next;
    }

    public void setNext(Handler next) {
        this.next = next;
    }

    abstract boolean process(Request request);
}

class RequestFrequentHandler extends Handler{

    public RequestFrequentHandler(Handler next) {
        super(next);
    }

    @Override
    boolean process(Request request) {
        System.out.println("访问频率控制------>");
        if (request.isFrequentOk()){
            System.out.println("访问频率控制通过");
            Handler next = getNext();
            if (null == next){
                return true;
            }else {
                //交由下一个handler节点处理
                return next.process(request);
            }
        }
        System.out.println("访问频率控制不通过");
        return request.isFrequentOk();
    }
}

class RequestPermitsHandler extends Handler{

    public RequestPermitsHandler(Handler next) {
        super(next);
    }

    @Override
    boolean process(Request request) {
        System.out.println("许可证控制------>");
        if (request.isPermits()){
            System.out.println("许可证控制通过");
            Handler next = getNext();
            if (null == next){
                return true;
            }else {
                //交由下一个handler节点处理
                return next.process(request);
            }
        }
        System.out.println("许可证控制不通过");
        return request.isPermits();
    }
}

class RequestLoggedOnHandler extends Handler{

    public RequestLoggedOnHandler(Handler next) {
        super(next);
    }

    @Override
    boolean process(Request request) {
        System.out.println("登录控制------>");
        if (request.isLoggedOn()){
            System.out.println("登录控制通过");
            Handler next = getNext();
            if (null == next){
                return true;
            }else {
                //交由下一个handler节点处理
                return next.process(request);
            }
        }
        System.out.println("登录控制不通过");
        return request.isLoggedOn();
    }
}

class RequestContainSensitiveWordsHandler extends Handler{

    public RequestContainSensitiveWordsHandler(Handler next) {
        super(next);
    }

    @Override
    boolean process(Request request) {
        System.out.println("敏感词控制------>");
        if (request.isContainSensitiveWords()){
            System.out.println("敏感词控制通过");
            Handler next = getNext();
            if (null == next){
                return true;
            }else {
                //交由下一个handler节点处理
                return next.process(request);
            }
        }
        System.out.println("敏感词控制不通过");
        return request.isContainSensitiveWords();
    }
}
